var rootPath = getRootPath();
var oriBankAccount; 

$(function() {
	
	$.ajax({
		url : rootPath + "/userDetailFront/queryDetailInfo",
		method : "GET",
		success : function(data) {
			if(data.returnCode == '0000') {
				if(data.returnObject != null) {
					var obj = data.returnObject;
					if(obj.accountName!=null && obj.accountName!=""){
						$("#accountName").val(obj.accountName);
					}
					if(obj.bankAccount!=null && obj.bankAccount!=""){
						$("#bankAccount").val(obj.bankAccount);
						oriBankAccount = $("#bankAccount").val();
					}
					if(obj.depositBank!=null && obj.depositBank!=""){
						$("#depositBank").val(obj.depositBank);
					}
					if(obj.aopenBranch!=null && obj.aopenBranch!=""){
						$("#aopenBranch").val(obj.aopenBranch);
					}
					if(obj.aopenProvince!=null && obj.aopenProvince!=""){
						getProvince(obj.aopenProvince,obj.aopenCity);
					}
					/*if(obj.aopenCity!=null && obj.aopenCity!=""){
						var aopenCity = obj.aopenCity;
						$("#aopenCity").val(aopenCity).select2();
					}*/
					
				}
			}
		},
		error: function (jqXHR, textStatus, errorThrown) {
			layer.alert("error");
		},
		complete:function(){
			if(!$("#aopenBranch").val() || $("#aopenBranch").val()==''){
				getProvince();
			}
		}
	});
	$("#aopenProvince").select2({
	 	placeholder:'请选择开户所在省',//默认文字提示
	 	language: "zh-CN",
	 	width: '19rem',
        tags: false,//允许手动添加
        allowClear: true,//允许清空
        escapeMarkup: function (markup) { return markup; }, // 自定义格式化防止xss注入
        maximumSelectionSizeint:1,
        formatResult: function formatRepo(repo){return repo.text;}, // 函数用来渲染结果
        formatSelection: function formatRepoSelection(repo){return repo.text;} // 函数用于呈现当前的选择
	});
	$("#aopenCity").select2({
	 	placeholder:'请选择开户所在市',//默认文字提示
	 	language: "zh-CN",
	 	width: '19rem',
        tags: false,//允许手动添加
        allowClear: true,//允许清空
        escapeMarkup: function (markup) { return markup; }, // 自定义格式化防止xss注入
        maximumSelectionSizeint:1,
        formatResult: function formatRepo(repo){return repo.text;}, // 函数用来渲染结果
        formatSelection: function formatRepoSelection(repo){return repo.text;} // 函数用于呈现当前的选择
	});
});
function getProvince(eP,eC){
	$.ajax({
		url : rootPath + "/area/pid/0",
		method : "GET",
		success : function(res) {
			if(res.returnCode == '0000') {
				var aopenProvince =  document.getElementById("aopenProvince"); 
				var provinceList = res.returnObject;
        		for(var i=0;i<provinceList.length;i++){
        			var province = provinceList[i];
        			var op= document.createElement("option");
        			op.innerHTML=province.title;
        			op.value=province.areaId;
        			aopenProvince.appendChild(op);
        		}
        		if(eP && eP !=""){
    				$("#aopenProvince").val(eP).select2();
    				getCity(eC);
    			}
			}
		},
		error: function (jqXHR, textStatus, errorThrown) {
			layer.alert("error");
		}
	});
}
function getCity(e){
	hideMsg("aopenProvince");
	hideMsg("aopenCity");
	
	$("#aopenCity").children(":gt(0)").remove();
	var p = $("#aopenProvince").val();
	if(!p ||p ==''){
		return;
	}
	$.ajax({
		url : rootPath + "/area/pid/"+p,
		method : "GET",
		success : function(res) {
			if(res.returnCode == '0000') {
				var aopenCity =  document.getElementById("aopenCity"); 
				var cityList = res.returnObject;
        		for(var i=0;i<cityList.length;i++){
        			var city = cityList[i];
        			var op= document.createElement("option");
        			op.innerHTML=city.title;
        			op.value=city.areaId;
        			aopenCity.appendChild(op);
        		}
			}
			if(e&&e!=""){
				$("#aopenCity").val(e).select2();
			}
		},
		error: function (jqXHR, textStatus, errorThrown) {
			layer.alert("error");
		}
	});
}